#!/usr/bin/env bash
# shellcheck disable=SC2145,SC2124
# shellcheck source=/dev/null

abs_path="$(builtin cd "$(dirname "${0}")" && builtin pwd)"
declare abs_path
source "${abs_path%/*}/libexec/lab-exec-calc"
source "${abs_path%/*}/libexec/lab-judge-args"
source "${abs_path%/*}/libexec/lab-error"

function dE94() {
    #Reference: https://ja.wikipedia.org/wiki/%E8%89%B2%E5%B7%AE#CIE94_(delta_E94)
    {
        judge "${@:2}"

        readonly k=${@:1:1}
        shift

        #* 重み付け係数
        readonly kc="1"
        readonly kh="1"
        if [[ "${k}" == "-g" ]]; then
            readonly kl="1"
            readonly k1="0.045"
            readonly k2="0.015"
        elif [[ "${k}" == "-t" ]]; then
            readonly kl="2"
            readonly k1="0.048"
            readonly k2="0.014"
        elif [[ "${k}" != "-g" ]] && [[ "${k}" != "-t" ]]; then
            error
        fi

        #* 基準色
        readonly L1=${@:1:1}
        readonly a1=${@:2:1}
        readonly b1=${@:3:1}

        #* 比較色
        readonly L2=${@:4:1}
        readonly a2=${@:5:1}
        readonly b2=${@:6:1}

        delta_L=$(calc "(${L1/L*=/} - ${L2/L*=/})")
        readonly delta_L

        c1=$(calc "sqrt((${a1/a*=/} ^ 2) + (${b1/b*=/} ^ 2))")
        readonly c1
        c2=$(calc "sqrt((${a2/a*=/} ^ 2) + (${b2/b*=/} ^ 2))")
        readonly c2
        delta_cab=$(calc "${c1} - ${c2}")
        readonly delta_cab

        delta_a=$(calc "(${a1/a*=/} - ${a2/a*=/})")
        readonly delta_a
        delta_b=$(calc "(${b1/b*=/} - ${b2/b*=/})")
        readonly delta_b

        delta_hab=$(calc "sqrt((${delta_a} ^ 2) + (${delta_b} ^ 2) - (${delta_cab} ^ 2))")
        readonly delta_hab

        readonly sl="1"
        sc=$(calc "(1 + (${k1} * ${c1}))")
        readonly sc
        sh=$(calc "(1 + (${k2} * ${c1}))")
        readonly sh

        a=$(calc "((${delta_L}/(${kl} * ${sl})) ^ 2)")
        readonly a
        b=$(calc "((${delta_cab}/(${kc} * ${sc})) ^ 2)")
        readonly b
        c=$(calc "((${delta_hab}/(${kh} * ${sh})) ^ 2)")
        readonly c

        calc "sqrt(${a} + ${b} + ${c})" | awk '{printf("%.4f\n",$1)}'
    }
}
